package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.common.constant.MessageConstant;
import com.atguigu.common.entity.PageResult;
import com.atguigu.common.entity.QueryPageBean;
import com.atguigu.common.entity.Result;
import com.atguigu.pojo.TravelGroup;
import com.atguigu.service.TravelGroupService;
import com.atguigu.vo.TravelGroupAddVo;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Locale;

@RestController
@RequestMapping("/travelGroup")
public class TravelGroupController {

    @Reference
    TravelGroupService travelGroupService;

    //分页查询旅游项信息
    @PostMapping("/selectPage")
    public Result travelSelectPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = travelGroupService.selectPage(queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString());
        return new Result(true,MessageConstant.QUERY_TRAVELGROUP_SUCCESS,pageResult);
    }
    //添加旅游项
    @PostMapping("/add")
    public Result travelgroupAdd(@RequestBody TravelGroupAddVo travelGroupAddVo){
        travelGroupService.groupAndItemAdd(travelGroupAddVo.getTravelGroup(),travelGroupAddVo.getIds());
        return new Result(true, MessageConstant.ADD_TRAVELGROUP_SUCCESS);
    }
    //根据id查询旅游项信息
    @GetMapping("/select/{id}")
    public Result selectId(@PathVariable("id")Integer id){
        TravelGroup travelGroup = travelGroupService.selectId(id);
        return new Result(true,MessageConstant.QUERY_TRAVELGROUP_SUCCESS,travelGroup);
    }

    //修改旅游项信息
    @PutMapping("/update")
    public Result travelUpdate(@RequestBody TravelGroupAddVo travelGroupAddVo){
        travelGroupService.groupUpdate(travelGroupAddVo.getTravelGroup(),travelGroupAddVo.getIds());
        return new Result(true,MessageConstant.EDIT_TRAVELGROUP_SUCCESS);
    }

    //查找旅游团关联的旅游信息id
    @GetMapping("/findGroupAndItem")
    public Result findGroupAndItem(@RequestParam("id")Integer id){
        List<Integer> ids = travelGroupService.findGroupAndItem(id);
        return new Result(true,MessageConstant.QUERY_TRAVELGROUP_SUCCESS,ids);
    }

    //查找所有
    @GetMapping("/selectAll")
    public Result selectAll(){
        List<TravelGroup> travelGroups = travelGroupService.selectAll();
        return new Result(true,MessageConstant.QUERY_TRAVELGROUP_SUCCESS,travelGroups);

    }
}
